package serve;

import Common.Message;
import Common.MessageType;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

public class ClientConnectServiceThread extends Thread{
    Socket socket;
    public ClientConnectServiceThread(Socket socket){
        this.socket=socket;
    }

    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {
        while (true){
            System.out.println("客户端的线程，等待读取从服务端的信息");
            try {//读取服务端的数据
                ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());

                Message message = (Message) ois.readObject();
                if (message.getMsgType().equals(MessageType.MESSAGE_ONLINE_FRIENDS)){

                }else if (message.getMsgType().equals(MessageType.MESSAGE_COMMON)){
                    System.out.println(message.getSender() + "对" + message.getReceiver()+ " 说：" + message.getContent());
                }else if (message.getMsgType().equals(MessageType.MESSAGE_COMMON_ALL)){
                    System.out.println(message.getSender() + " 说：" + message.getContent());

                }else if (message.getMsgType().equals(MessageType.MESSAGE_FILE_SEND)){
                    System.out.println(message.getSender() + "发送文件"+message.getSrc()+"在"+message.getDest());
                    FileOutputStream fos = new FileOutputStream(message.getDest());
                    fos.write(message.getFileBytes());
                    fos.close();
                    System.out.println("成功");

                }

            } catch (Exception e) {
                e.getMessage();
            }
        }
    }


}
